//package com.open.cloud.rbac.handler;
//
//import com.alibaba.fastjson.JSONObject;
//import com.inspur.cloud.handler.CloudInterfaceCheckHandler;
//import com.inspur.micro.core.security.JwtConstants;
//import com.inspur.micro.core.security.SecurityUtil;
//import com.inspur.micro.core.security.TokenProvider;
//import com.inspur.micro.core.utils.SpringContextUtil;
//import com.open.cloud.rbac.service.application.ICoreApplicationInterfaceService;
//import io.jsonwebtoken.Claims;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.ObjectUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.env.Environment;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import java.util.HashMap;
//import java.util.Map;
//
//@Slf4j
//@Component
//public class CloudRbacInterfaceCheckHandler extends CloudInterfaceCheckHandler {
//
//    @Resource
//    private TokenProvider tokenProvider;
//
//    @Override
//    public void selectEntityInfo(Map<String, Object> entityInfoMap) {
//        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(entityInfoMap);
//        System.out.println(jsonObject);
//    }
//
//    @Override
//    public Boolean check(String url) {
//        //获取RequestAttributes
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        //从获取到的RequestAttributes中获取HttpServletRequest的信息
//        HttpServletRequest httpRequest = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
//
//        //请求的参数
//        Map<String, String> rtnMap = converMap(httpRequest.getParameterMap());
//        String source = rtnMap.get("filterSource");
//        if (!(ObjectUtils.isNotEmpty(source) && source.equals("inspur"))) {
//            try {
//                String userId = getUserId();
//                ICoreApplicationInterfaceService interfaceService = SpringContextUtil.getBean(ICoreApplicationInterfaceService.class);
//                Environment env = SpringContextUtil.getBean(Environment.class);
//                String applicationName = env.getProperty("spring.application.name");
//
//                Boolean checkResult = interfaceService.checkUserInterfacePermission(userId, applicationName, url);
//
//                return checkResult;
//
//            } catch (Exception e) {
//                log.error(e.getLocalizedMessage(), e);
//                return false;
//            }
//        } else {
//            return true;
//        }
//
//    }
//
//    public String getUserId() throws Exception {
//        String jwt = SecurityUtil.getCurrentUserJWT().get();
//        Claims claims = tokenProvider.parseToken(jwt).get();
//        String userId = claims != null ? (String) claims.getOrDefault(JwtConstants.LOGIN_USER_ID, "") : null;
//        return userId;
//    }
//
//    public Map<String, String> converMap(Map<String, String[]> paramMap) {
//        Map<String, String> rtnMap = new HashMap<>();
//        for (String key : paramMap.keySet()) {
//            rtnMap.put(key, paramMap.get(key)[0]);
//        }
//        return rtnMap;
//    }
//
//}
